package com.css.fxfzdzzh.modules.taskManage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.constants.FxfzConstants;
import com.css.fxfzdzzh.modules.taskManage.entity.TaskDistrictArea;
import com.css.fxfzdzzh.modules.taskManage.entity.TaskEntity;
import com.css.fxfzdzzh.modules.taskManage.service.TaskService;
import com.css.fxfzdzzh.modules.taskManage.vo.*;
import com.css.fxfzdzzh.util.PlatformObjectUtils;
import com.css.fxfzdzzh.util.PlatformPageUtils;
import com.css.fxfzdzzh.util.ResponseCreator;
import org.apache.tools.ant.taskdefs.Taskdef;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年02月13日  14:08
 */
@RestController
@RequestMapping("/task")
@Validated
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 新建任务
     */
    @PostMapping("/add")
    public RestResponse addTask(@RequestBody TaskVO vo) throws Exception {
        taskService.saveTask(vo);
        return ResponseCreator.createSuccessResponse("创建成功");
    }

    /***
     * 任务列表查询
     * @param taskVo
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(TaskQueryVO taskVo, HttpServletRequest request){
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=taskService.getPage(taskVo,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /**
     * 修改任务
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody TaskUpdateVO vo){
        taskService.update(vo);
        return ResponseCreator.createSuccessResponse("修改成功");
    }

    /**
     * 删除任务
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody TaskDeleteVO vo){
        if(vo.getIds().size()==0 ){
            return ResponseCreator.createFailResponse("请选择要删除的数据");
        }
        taskService.delete(vo.getIds());
        return ResponseCreator.createSuccessResponse("删除成功");
    }

    /**
     * 查看任务
     */
    @GetMapping("/viewTask")
    public RestResponse viewTask(@NotBlank(message = "任务id不能为空")String dtId){
        return ResponseCreator.createSuccessResponse(taskService.viewTask(dtId));
    }

    /**
     * 查看任务面积
     */
    @GetMapping("/viewTaskArea")
    public RestResponse viewTaskArea(@NotBlank(message = "任务id不能为空")String dtId){
        return ResponseCreator.createSuccessResponse(taskService.viewTaskArea(dtId));
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, String dtId) throws Exception {
        taskService.exportExcel(dtId,response);
    }

    /**
     * 获取务编号
     * @return
     */
    @GetMapping("/taskCode")
    public RestResponse getTaskCode(){
        return ResponseCreator.createSuccessResponse(taskService.getTaskCode());
    }

    /**
     * 根据地图圈选区域，查询行政区划--省|市|区县的名称和code列表
     */
    @PostMapping("/queryProvinceCityCountyNameCodeListByGeomText")
    public RestResponse queryProvinceCityCountyNameCodeListByGeomText(@RequestBody Map<String,String> map){

        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(taskService.queryProvinceCityCountyNameCodeListByGeomText(map.get("geomText")));
        }catch (Exception e){
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /**
     * 根据地图圈选区域，查询行政区划--省的名称和code列表
     */
    @PostMapping("/queryProvinceNameCodeListByGeomText")
    public RestResponse queryProvinceNameCodeListByGeomText(@RequestBody Map<String,String> map){

        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(taskService.queryProvinceNameCodeListByGeomText(map.get("geomText")));
        }catch (Exception e){
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /***
     * 查询所有父任务
     * @param
     * @return
     */
    @GetMapping("/getTask")
    public RestResponse getTask(){
        RestResponse response = null;
        try{
            List<TaskEntity> taskList=taskService.getTask();
            response = RestResponse.succeed(taskList);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

}
